package com.springboot.mongo.config;


import com.mongodb.WriteConcern;
import com.springboot.common.entity.request.Validate;
import com.springboot.common.util.StringUtil;
import lombok.Data;
import org.springframework.data.mongodb.core.WriteConcernResolver;

/**
 * mongo配置器
 *
 * @author supu
 * @date 2019-08-26 10:38
 **/
@Data
public class MongoConfigurer implements Validate {
    /**
     * 当前环境
     */
    private String env;
    /**
     * 主机地址
     */
    private String host;
    /**
     * 用户名
     */
    private String userName;
    /**
     * 密码
     */
    private String password;
    /**
     * 数据库名
     */
    private String dataBaseName;
    /**
     * 数据库连接超时时间
     */
    private static final Integer DEFAULT_CONNECT_TIMEOUT = 3000;
    /**
     * socket连接超时时间
     */
    private static final Integer DEFAULT_SOCKET_TIMEOUT = 0;
    /**
     * 每个地址最大的请求数
     */
    private static final Integer DEFAULT_CONNECTION_SPER_HOST = 30;
    /**
     * 最大等待时间
     */
    private static final Integer DEFAULT_MAX_WAIT_TIME = 0;
    /**
     * 一个socket最大的等待请求数
     */
    private static final Integer DEFAULT_THREADS_ALLOWED_TO_BLOCK_FOR_CONNECTION_MULTIPLIER = 5;
    /**
     * 写入关注等级
     */
    private static final WriteConcern DEFAULT_WRITE_CONCERN = WriteConcern.ACKNOWLEDGED;
    /**
     * 读取数据策略
     */
    private static final String DEFAULT_READ_PREFERENCE = "primaryPreferred";
    /**
     * 是否带上_class信息，默认带上
     */
    private static final boolean DEFAULT_CONVERT_WITH_CLASS_INFO = true;
    /**
     * 是否带上_class信息，默认带上
     */
    private boolean convertWithClassInfo;
    /**
     * 写入关系解析器
     */
    private WriteConcernResolver writeConcernResolver;
    /**
     * 最近优先策略
     */
    private String readPreference;
    /**
     * 连接超时时间
     */
    private Integer connectTimeout;
    /**
     * socket连接超时时间
     */
    private Integer socketTimeout;
    /**
     * 每个地址最大请求数
     */
    private Integer connectionsPerHost;
    /**
     * 长链接的最大等待时间
     */
    private Integer maxWaitTime;
    /**
     * 一个socket最大的等待请求数
     */
    private Integer threadsAllowedToBlockForConnectionMultiplier;
    /**
     * 写入关注等级
     */
    private WriteConcern writeConcern;
    /**
     * 集群秘钥
     */
    private String replicaSetName;

    public MongoConfigurer(String userName, String password, String host, String dataBaseName) {
        this.readPreference = DEFAULT_READ_PREFERENCE;
        this.connectionsPerHost = DEFAULT_CONNECTION_SPER_HOST;
        this.connectTimeout = DEFAULT_CONNECT_TIMEOUT;
        this.socketTimeout = DEFAULT_SOCKET_TIMEOUT;
        this.maxWaitTime = DEFAULT_MAX_WAIT_TIME;
        this.threadsAllowedToBlockForConnectionMultiplier = DEFAULT_THREADS_ALLOWED_TO_BLOCK_FOR_CONNECTION_MULTIPLIER;
        this.writeConcern = DEFAULT_WRITE_CONCERN;
        this.convertWithClassInfo = DEFAULT_CONVERT_WITH_CLASS_INFO;
        this.userName = userName;
        this.password = password;
        this.host = host;
        this.dataBaseName = dataBaseName;
    }

    @Override
    public boolean validate() {
        return StringUtil.isNotBlank(host) && StringUtil.isNotBlank(dataBaseName);
    }
}
